﻿using Inovout.Cloud.DataStore.QuickStarts.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace Inovout.Cloud.DataStore.QuickStarts
{
    class Program
    {
        static string baseUri = "http://localhost:10230/";//"http://datastore.3cloud.com/";
        static string databaseName = "persontest";
        static string tableName = "Student";
        static string properties = "{\"Properties\":[{\"Name\":\"PersonId\",\"Type\":\"Int16\",\"isPrimaryKey\":\"true\"},"
                                    + "{\"Name\":\"Name\",\"Type\":\"String\",\"isPrimaryKey\":\"false\"},{\"Name\":\"Birthday\",\"Type\":\"DateTime\",\"isPrimaryKey\":\"false\"}],"
                                    + "\"Name\":\"" + tableName + "\"}";
        static void Main(string[] args)
        {
            //Console.WriteLine("--------------开始创建表-------------------------------");
            //CreateTable(databaseName, tableName, properties);
            //Console.WriteLine("--------------结束创建表-------------------------------");

            //Console.WriteLine("--------------开始查询表结构-------------------------------");
            //GetTableSchema(databaseName, tableName);
            //Console.WriteLine("--------------结束查询表结构-------------------------------");

            //Console.WriteLine("--------------开始删除表-------------------------------");
            //DeleteTable(databaseName, tableName);
            //Console.WriteLine("--------------结束删除表-------------------------------");

            //Console.WriteLine("--------------开始创建表-------------------------------");
            //CreateTable(databaseName, tableName, properties);
            //Console.WriteLine("--------------结束创建表-------------------------------");

            //StudentInfo studentInfo = new StudentInfo() { PersonId = 2, Name = "zhu", Birthday = DateTime.Now };
            //StudentInfo studentInfo1 = new StudentInfo() { PersonId = 1, Name = "zhu1", Birthday = DateTime.Now };
            //StudentInfo studentInfo2 = new StudentInfo() { PersonId = 9, Name = "zhu9", Birthday = DateTime.Now };
            //StudentInfo studentInfo3 = new StudentInfo() { PersonId = 3, Name = "zhu3", Birthday = DateTime.Now };
            //StudentInfo studentInfo4 = new StudentInfo() { PersonId = 4, Name = "zhu4", Birthday = DateTime.Now };
            //StudentInfo studentInfo5 = new StudentInfo() { PersonId = 5, Name = "zhu5", Birthday = DateTime.Now };
            //StudentInfo studentInfo6 = new StudentInfo() { PersonId = 6, Name = "zhu6", Birthday = DateTime.Now };
            //StudentInfo studentInfo7 = new StudentInfo() { PersonId = 7, Name = "zhu7", Birthday = DateTime.Now };
            //StudentInfo studentInfo8 = new StudentInfo() { PersonId = 8, Name = "zhu8", Birthday = DateTime.Now };
            //Console.WriteLine("--------------插入记录-------------------------------");
            //InsertEntity(databaseName, tableName, studentInfo);
            //InsertEntity(databaseName, tableName, studentInfo1);
            //InsertEntity(databaseName, tableName, studentInfo2);
            //InsertEntity(databaseName, tableName, studentInfo3);
            //InsertEntity(databaseName, tableName, studentInfo4);
            //InsertEntity(databaseName, tableName, studentInfo5);
            //InsertEntity(databaseName, tableName, studentInfo6);
            //InsertEntity(databaseName, tableName, studentInfo7);
            //InsertEntity(databaseName, tableName, studentInfo8);
            //Console.WriteLine("--------------插入记录-------------------------------");


            //Console.WriteLine("--------------修改记录-------------------------------");
            //studentInfo2.Name = "zhuxiang";
            //UpdateEntity(databaseName, tableName, studentInfo2);

            //Console.WriteLine("--------------修改记录-------------------------------");

            //Console.WriteLine("--------------查询记录-------------------------------");
            //GetAllEntitiesByTable(databaseName, tableName);
            //Console.WriteLine("--------------查询记录-------------------------------");

            //Console.WriteLine("--------------删除记录-------------------------------");
            //var key = "8";
            //DeleteEntity(databaseName, tableName, key);
            //Console.WriteLine("--------------删除记录-------------------------------");

            //Console.WriteLine("--------------条件查询记录-------------------------------");
            //GetEntitiesByConditions(databaseName, tableName, "PersonId eq 5");
            //GetEntitiesByConditions(databaseName, tableName, "PersonId eq 9 or PersonId eq 5");
            //GetEntitiesByConditions(databaseName, tableName, "Name eq  'zhuxiang'");//字段区分大小写
            //Console.WriteLine("--------------条件查询记录-------------------------------");

            //Console.WriteLine("--------------分页查询-------------------------------");
            //todo:分页查询
            //Console.WriteLine("--------------分页查询-------------------------------");

           

            Console.ReadLine();
        }

        /// <summary>
        /// 创建MongoDB的表
        /// </summary>
        /// <param name="databaseName">数据库名</param>
        /// <param name="tableName">表名</param>
        /// <param name="properties">字段</param>
        /// <returns></returns>
        private static string CreateTable(string databaseName, string tableName, string properties)
        {
            HttpClient httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            var content = new StringContent(properties, Encoding.UTF8, "application/json");
            UriBuilder build = new UriBuilder(baseUri);
            build.Path = "/v1/t/db/" + databaseName;
            var result = httpClient.PutAsync(build.ToString(), content);
            var message = result.Result.Content.ReadAsStringAsync().Result;
            return message;
        }


        /// <summary>
        /// 删除表
        /// </summary>
        /// <param name="databaseName">数据库名</param>
        /// <param name="tableName">表名</param>
        /// <returns></returns>
        private static string DeleteTable(string databaseName, string tableName)
        {
            HttpClient httpClient = new HttpClient();
            UriBuilder build = new UriBuilder(baseUri);
            build.Path = "/v1/t/db/" + databaseName + "/t/" + tableName;
            var result = httpClient.DeleteAsync(build.ToString());
            var message = result.Result.Content.ReadAsStringAsync().Result;
            if (message == null)
            {
                return "success";
            }
            else
            {
                return "fail";
            }
        }

        /// <summary>
        /// 查询表的schema
        /// </summary>
        /// <param name="databaseName">数据库名</param>
        /// <param name="tableName">表名</param>
        /// <returns></returns>
        private static string GetTableSchema(string databaseName, string tableName)
        {
            HttpClient httpClient = new HttpClient();
            UriBuilder build = new UriBuilder(baseUri);
            build.Path = "/v1/t/db/" + databaseName + "/t/" + tableName;
            var result = httpClient.GetAsync(build.ToString());
            var message = result.Result.Content.ReadAsStringAsync().Result;
            return message;
        }


        /// <summary>
        /// 向表中插入实体
        /// </summary>
        /// <param name="databaseName">数据库名</param>
        /// <param name="tableName">表名</param>
        /// <param name="entity">记录</param>
        /// <returns></returns>
        private static string InsertEntity(string databaseName, string tableName, object entity)
        {
            HttpClient httpClient = new HttpClient();
            var entityJson = Newtonsoft.Json.JsonConvert.SerializeObject(entity);
            var content = new StringContent(entityJson, Encoding.UTF8, "application/json");
            UriBuilder build = new UriBuilder(baseUri);
            build.Path = "/v1/e/db/" + databaseName + "/t/" + tableName;
            var result = httpClient.PutAsync(build.ToString(), content);
            var message = result.Result.Content.ReadAsStringAsync().Result;
            return message;
        }


        /// <summary>
        /// 修改实体内容
        /// </summary>
        /// <param name="databaseName">数据库名</param>
        /// <param name="tableName">表名</param>
        /// <param name="entity">记录</param>
        /// <returns></returns>
        private static string UpdateEntity(string databaseName, string tableName, object entity)
        {
            HttpClient httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            var entityJson = Newtonsoft.Json.JsonConvert.SerializeObject(entity);
            var content = new StringContent(entityJson, Encoding.UTF8, "application/json");
            UriBuilder build = new UriBuilder(baseUri);
            build.Path = "v1/e/db/" + databaseName + "/t/" + tableName;
            var result = httpClient.PostAsync(build.ToString(), content);
            var message = result.Result.Content.ReadAsStringAsync().Result;
            return message;
        }


        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="databaseName">数据库名</param>
        /// <param name="tableName">表名</param>
        /// <param name="key">记录主键</param>
        /// <returns></returns>
        private static string DeleteEntity(string databaseName, string tableName, string key)
        {
            HttpClient httpClient = new HttpClient();
            UriBuilder build = new UriBuilder(baseUri);
            build.Path = "v1/e/db/" + databaseName + "/t/" + tableName + "/k/" + key;
            var result = httpClient.DeleteAsync(build.ToString());
            var message = result.Result.Content.ReadAsStringAsync().Result;
            return message;
        }


        /// <summary>
        /// 查询表中的所有实体
        /// </summary>
        /// <param name="databaseName">数据库名</param>
        /// <param name="tableName">表名</param>
        /// <returns></returns>
        private static string GetAllEntitiesByTable(string databaseName, string tableName)
        {

            HttpClient httpClient = new HttpClient();
            UriBuilder build = new UriBuilder(baseUri);
            build.Path = "/v1/e/db/" + databaseName + "/t/" + tableName;
            var result = httpClient.GetAsync(build.ToString());
            var message = result.Result.Content.ReadAsStringAsync().Result;
            return message;
        }

        /// <summary>
        /// 条件过滤查询
        /// </summary>
        /// <param name="databaseName">数据库名</param>
        /// <param name="tableName">表名</param>
        /// <param name="condtions">过滤条件</param>
        /// <returns></returns>
        private static string GetEntitiesByConditions(string databaseName, string tableName, string condtions)
        {
            HttpClient httpClient = new HttpClient();
            UriBuilder build = new UriBuilder(baseUri);
            build.Path = "v1/e/query/db/" + databaseName + "/t/" + tableName;
            build.Query = "$filter=" + condtions;
            var result = httpClient.GetAsync(build.ToString());
            var message = result.Result.Content.ReadAsStringAsync().Result;
            return message;
        }
    }

   
   
}
